package com.ql.service.serviceImpl;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service(value = "excelService")
@Transactional
public class ExcelServiceImpl {
    private static int currRows = 0;// 当前所在行

    public void WriteColsCell(HSSFSheet st, HSSFCellStyle normalStyle, String ruleStr, String[] letters) {
        String[] temp = ruleStr.split(";");
        int[] rules = new int[temp.length];

        for (int i = 0; i < temp.length; i++) {
            rules[i] = Integer.parseInt(temp[i]);
        }

        HSSFRow row = st.createRow(currRows);
        st.setDefaultColumnWidth(16);
        //设置行高
        row.setHeightInPoints(24);
        int preCols = 0;
        int a = 0;
//            int colorInt=0;//当前行颜色
        for (int i = 0; i < rules.length; i++) {
            st.addMergedRegion(new Region(currRows, (short) preCols, currRows, (short) (preCols + rules[i] - 1)));
            HSSFCell cell = row.createCell((short) preCols);

            cell.setCellValue(new HSSFRichTextString(letters[i]));
            cell.setCellStyle(normalStyle);
            a += rules[i];
            for (int j = preCols + 1; j < a; j++) {
                row.createCell((short) j).setCellStyle(normalStyle);
            }

            preCols = preCols + rules[i];
        }
        currRows++;
    }


    public void WriteRowsCell(HSSFSheet st, HSSFCellStyle normalStyle, String ruleStr, String[] letters) {
        String[] temp = ruleStr.split(";");
        int[] rules = new int[temp.length];

        for (int i = 0; i < temp.length; i++) {
            rules[i] = Integer.parseInt(temp[i]);
        }

        int curCols = 0;//当前所在列
        int curLetters = 0;//当前字符值
        int tempRows = 0;
        for (int i = 0; i < rules.length; i++) {
            if (rules[i] != -1) {
                if (rules[i] > 1) tempRows = rules[i];
                st.addMergedRegion(new Region(currRows, (short) curCols, currRows + rules[i] - 1, (short) curCols));
                HSSFRow row = st.createRow(currRows);
                HSSFCell cell = row.createCell((short) curCols);
                cell.setCellValue(new HSSFRichTextString(letters[curLetters++]));
                cell.setCellStyle(normalStyle);
                curCols++;
            } else {
                currRows++;
                if ((tempRows--) > 1) curCols = 1;
            }
        }
    }

    public HSSFWorkbook export() {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet st = wb.createSheet("0+0_248-9未知桩号_原始记录表-综合版");

        // 这里首先创建一个单元格样式对象，设置了四周的边框以及字体可以换行
        // 其中的字体换行是用来竖向显示其中的一个单元格的
        // 更好的一点儿做法是再做一个单独的单元格样式对象
        // 要不然在处理自动列宽的时候可能会有点儿小问题
        HSSFCellStyle normalStyle = wb.createCellStyle();
        normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        normalStyle.setWrapText(true);
        HSSFFont normalFont = wb.createFont();
        normalFont.setFontHeightInPoints((short) 14);//设置字体大小
        normalFont.setFontName("宋体");//设置字体
        normalFont.setColor(HSSFColor.BLACK.index);
        normalFont.setBoldweight((short) 0.8);
        normalFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        normalStyle.setFont(normalFont);


        HSSFCellStyle leftStyle = wb.createCellStyle();//背景
        leftStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        leftStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        leftStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        leftStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        leftStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        leftStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        leftStyle.setWrapText(true);

        HSSFCellStyle centerStyle = wb.createCellStyle();//背景
        centerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        centerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        centerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        centerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        centerStyle.setWrapText(true);


        WriteColsCell(st, normalStyle, "8", new String[]{"水泥土搅拌桩施工现场原始记录表（详细版）"});
        WriteColsCell(st, leftStyle, "8", new String[]{"工程名称：二标段（K2+240～K5+500）"});
        WriteColsCell(st, leftStyle, "4;4", new String[]{"施工单位：中国建筑第五工程局有限公司", "桩机编号：16"});
        WriteColsCell(st, leftStyle, "4;2;1;1", new String[]{"里程桩号：0+0_248/9", "桩坐标：", "118.295784870725", "24.59053045"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"设计总桩长(m)：", "", "水灰比：", ""});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"水泥掺量（kg/m）:", "", "桩间距(m)：", ""});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"提钻速度（cm/min）:", "", "下钻速度（cm/min）:", ""});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"桩径（cm):", "", "空搅深度（m）：", ""});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"开始时间：", "2020/12/15  7:56:30", "结束时间：", "2020/12/15  8:37:06"});
        WriteColsCell(st, centerStyle, "1;1;1;1;1;1;1;1", new String[]{"深度(m)", "段浆量(L/m)", "段灰量(kg/m)", "段密度(g/cm³)", "段电流(A)", "段喷压(MPa)", "速率(cm/min)", "状态"});
        for (int i = 0; i < 10; i++) {
            WriteColsCell(st, centerStyle, "1;1;1;1;1;1;1;1", new String[]{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"});
        }
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"成桩总时间(s)：", "2437", "最大斜度：", "0"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"往复深度1（m）:", "9.57", "往复深度2（m）:", "0"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"累计浆量（L）：", "883.2", "累计灰量（kg）：", "997.16"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"最大电流（A）:", "40.9", "平均灰量（kg/m）：", "104.19"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"最大提速（cm/min）：", "95", "最大钻速（cm/min）：", "92"});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"操作人员：", "", "技术人员：", ""});
        WriteColsCell(st, leftStyle, "2;2;2;2", new String[]{"现场监理：", "", "打印时间：", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())});

        return wb;

    }
}
